﻿Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports Microsoft.Win32

<Serializable()> _
Public Class InitSettings
    '設定を保存するフィールド

    'POPサーバー
    Private _popServer As String
    'SMTPサーバー
    Private _smtpServer As String
    '受信SSLの利用可否
    Private _popSsl As String
    '送信SSLの利用可否
    Private _smtpSsl As String
    'ユーザーID
    Private _userId As String
    'パスワード
    Private _password As String
    'メール受信間隔(ミリ秒)
    Private _interval As String
    'POPポート
    Private _popPort As String
    'SMTPポート
    Private _smtpPort As String
    'ドメイン
    Private _domain As String
    'リトライ回数
    Private _retryCount As String
    'リトライ間隔(ミリ秒)
    Private _retryTime As String
    '監視メール送信間隔
    Private _sendMailInterval As String
    'バックアップファイルパス
    Private _outputPath As String
    '復号化キー
    Private _privateKeyPath As String
    '宛先不明エラーメールタイトル
    Private _sendErrMailTitle As String
    ''DB取込み機能エラーメールタイトル
    'Private _DBErrMailTitle As String

    'メーリングリスト
    Private _mailingList() As MailingItems

    '設定のプロパティ
    Public Property PopServer() As String
        Get
            Return _popServer
        End Get
        Set(ByVal Value As String)
            _popServer = Value
        End Set
    End Property

    Public Property SmtpServer() As String
        Get
            Return _smtpServer
        End Get
        Set(ByVal Value As String)
            _smtpServer = Value
        End Set
    End Property

    Public Property PopSsl() As String
        Get
            Return _popSsl
        End Get
        Set(ByVal Value As String)
            _popSsl = Value
        End Set
    End Property
    Public Property SmtpSsl() As String
        Get
            Return _smtpSsl
        End Get
        Set(ByVal Value As String)
            _smtpSsl = Value
        End Set
    End Property

    Public Property UserId() As String
        Get
            Return _userId
        End Get
        Set(ByVal Value As String)
            _userId = Value
        End Set
    End Property

    Public Property Password() As String
        Get
            Return _password
        End Get
        Set(ByVal Value As String)
            _password = Value
        End Set
    End Property
    Public Property Interval() As String
        Get
            Return _interval
        End Get
        Set(ByVal Value As String)
            _interval = Value
        End Set
    End Property
    Public Property PopPort() As String
        Get
            Return _popPort
        End Get
        Set(ByVal Value As String)
            _popPort = Value
        End Set
    End Property
    Public Property SmtpPort() As String
        Get
            Return _smtpPort
        End Get
        Set(ByVal Value As String)
            _smtpPort = Value
        End Set
    End Property
    Public Property Domain() As String
        Get
            Return _domain
        End Get
        Set(ByVal Value As String)
            _domain = Value
        End Set
    End Property
    Public Property RetryCount() As String
        Get
            Return _retryCount
        End Get
        Set(ByVal Value As String)
            _retryCount = Value
        End Set
    End Property
    Public Property RetryTime() As String
        Get
            Return _retryTime
        End Get
        Set(ByVal Value As String)
            _retryTime = Value
        End Set
    End Property
    Public Property SendMailInterval() As String
        Get
            Return _sendMailInterval
        End Get
        Set(ByVal Value As String)
            _sendMailInterval = Value
        End Set
    End Property
    Public Property OutputPath() As String
        Get
            Return _outputPath
        End Get
        Set(ByVal Value As String)
            _outputPath = Value
        End Set
    End Property
    Public Property PrivateKeyPath() As String
        Get
            Return _privateKeyPath
        End Get
        Set(ByVal Value As String)
            _privateKeyPath = Value
        End Set
    End Property
    Public Property SendErrMailTitle() As String
        Get
            Return _sendErrMailTitle
        End Get
        Set(ByVal Value As String)
            _sendErrMailTitle = Value
        End Set
    End Property
    'Public Property DBErrMailTitle() As String
    '    Get
    '        Return _DBErrMailTitle
    '    End Get
    '    Set(ByVal Value As String)
    '        _DBErrMailTitle = Value
    '    End Set
    'End Property
    Public Property MailingList() As MailingItems()
        Get
            Return _mailingList
        End Get
        Set(ByVal Value As MailingItems())
            _mailingList = Value
        End Set
    End Property
    'メーリングリスト
    Public Structure MailingItems
        '対象システム名
        Private _systemName As String
        'アプリケーション名
        Private _applicationName As String
        '検索用キー
        'Private _searchKey As String
        '宛先メール
        Private _acceptMail As String
        '宛先の名前
        Private _acceptName As String
        '件名
        Private _titleName As String
        '差出人名
        Private _sendName As String
        'タイム略称
        Private _timeAbbreviation As String
        '時差
        Private _timeDifference As String

        'OpenSSLキー
        Private _openSslKeyList() As OpenSslKeyItems

        '対象初期化
        Public Sub New(ByVal _systemName As String, _
                       ByVal _applicationName As String, _
                       ByVal _acceptMail As String, _
                       ByVal _acceptName As String, ByVal _titleName As String, ByVal _sendName As String, _
                       ByVal _timeAbbreviation As String, ByVal _timeDifference As String, _
                       ByVal _openSslKeyList() As OpenSslKeyItems)
            Me._systemName = _systemName
            Me._applicationName = _applicationName
            Me._acceptMail = _acceptMail
            Me._acceptName = _acceptName
            Me._titleName = _titleName
            Me._timeAbbreviation = _timeAbbreviation
            Me._timeDifference = _timeDifference
            Me._openSslKeyList = _openSslKeyList

        End Sub

        Public Property SystemName() As String
            Get
                Return _systemName
            End Get
            Set(ByVal value As String)
                _systemName = value
            End Set
        End Property

        Public Property ApplicationName() As String
            Get
                Return _applicationName
            End Get
            Set(ByVal value As String)
                _applicationName = value
            End Set
        End Property
        '
        Public Property AcceptMail As String
            Get
                Return _acceptMail
            End Get
            Set(ByVal value As String)
                _acceptMail = value
            End Set
        End Property
        '
        Public Property AcceptName As String
            Get
                Return _acceptName
            End Get
            Set(ByVal value As String)
                _acceptName = value
            End Set
        End Property
        Public Property TitleName As String
            Get
                Return _titleName
            End Get
            Set(ByVal value As String)
                _titleName = value
            End Set
        End Property
        Public Property SendName As String
            Get
                Return _sendName
            End Get
            Set(ByVal value As String)
                _sendName = value
            End Set
        End Property
        Public Property TimeAbbreviation As String
            Get
                Return _timeAbbreviation
            End Get
            Set(ByVal value As String)
                _timeAbbreviation = value
            End Set
        End Property
        Public Property TimeDifference As String
            Get
                Return _timeDifference
            End Get
            Set(ByVal value As String)
                _timeDifference = value
            End Set
        End Property
        Public Property OpenSslKeyList() As OpenSslKeyItems()
            Get
                Return _openSslKeyList
            End Get
            Set(ByVal Value As OpenSslKeyItems())
                _openSslKeyList = Value
            End Set
        End Property

    End Structure

    'OpenSSLキー
    Public Structure OpenSslKeyItems
        '公開鍵
        Private _publicKey As String
        '秘密鍵
        Private _privateKey As String
        'パスフレーズ
        Private _passphrase As String
        '対象初期化
        Public Sub New(ByVal _publicKey As String, ByVal _privateKey As String)
            Me._publicKey = _publicKey
            Me._privateKey = _privateKey
        End Sub

        Public Property PublicKey() As String
            Get
                Return _publicKey
            End Get
            Set(ByVal value As String)
                _publicKey = value
            End Set
        End Property

        Public Property PrivateKey() As String
            Get
                Return _privateKey
            End Get
            Set(ByVal value As String)
                _privateKey = value
            End Set
        End Property
        Public Property Passphrase() As String
            Get
                Return _passphrase
            End Get
            Set(ByVal value As String)
                _passphrase = value
            End Set
        End Property
    End Structure

    'コンストラクタ
    Public Sub New()
        '_smtpServer = "172.28.73.85"
        '_popServer = "172.28.73.85"
        '_useSsl = True
        '_userId = "qin"
        '_password = "quartz"
        ''_smtpServer = "smtp.mail.yahoo.co.jp"
        ''_popServer = "pop.mail.yahoo.co.jp"
        ''_userId = "cebuwadi"
        ''_password = "quartz"
        '_popPort = 996
        '_smtpPort = 26
        '_uid = 0
        '_domain = "wadi.com"
        '_interval = 1000 * 5

        '_retryCount = 3
        '_retryTime = 1000 * 10


        ''メーリングリスト
        '_mailingList = New MailingItems(3) { _
        '    New MailingItems("ﾀﾞｲﾚｸﾄ", "group1@wadi.com", "group1", "件名テスト", "差出人の名前テスト", _
        '                     New OpenSslKeyItems(1) { _
        '                        New OpenSslKeyItems("OpenSslKey\PublicKey2012_1.pem", "OpenSslKey\PrivateKey2012_1.pem"), _
        '                        New OpenSslKeyItems("OpenSslKey\PublicKey2013_1.pem", "OpenSslKey\PrivateKey2013_1.pem")}), _
        '    New MailingItems("法人web", "group2@wadi.com", "group2", "", "", _
        '                     New OpenSslKeyItems(1) { _
        '                        New OpenSslKeyItems("OpenSslKey\PublicKey2012_2.pem", "OpenSslKey\PrivateKey2012_2.pem"), _
        '                        New OpenSslKeyItems("OpenSslKey\PublicKey2013_2.pem", "OpenSslKey\PrivateKey2013_2.pem")}), _
        '    New MailingItems("GCMS", "group3@wadi.com", "group3", "", "", _
        '                     New OpenSslKeyItems(1) { _
        '                        New OpenSslKeyItems("OpenSslKey\PublicKey2012_3.pem", "OpenSslKey\PrivateKey2012_3.pem"), _
        '                        New OpenSslKeyItems("OpenSslKey\PublicKey2013_3.pem", "OpenSslKey\PrivateKey2013_3.pem")}), _
        '    New MailingItems("ﾋﾞｼﾞﾈｽweb", "group4@wadi.com", "group4", "", "", _
        '                     New OpenSslKeyItems(1) { _
        '                        New OpenSslKeyItems("OpenSslKey\PublicKey2012_4.pem", "OpenSslKey\PrivateKey2012_4.pem"), _
        '                        New OpenSslKeyItems("OpenSslKey\PublicKey2013_4.pem", "OpenSslKey\PrivateKey2013_4.pem")})
        '}

        ''OpenSSLキー
        '_openSslKeyList = New OpenSslKeyItems(1) { _
        '    New OpenSslKeyItems("OpenSslKey\PublicKey2012.pem", "OpenSslKey\PrivateKey2012.pem"), _
        '    New OpenSslKeyItems("OpenSslKey\PublicKey2013.pem", "OpenSslKey\PrivateKey2013.pem")}
    End Sub

    'Settingsクラスのただ一つのインスタンス
    <NonSerialized()> _
    Private Shared _instance As InitSettings
    <System.Xml.Serialization.XmlIgnore()> _
    Public Shared Property Instance() As InitSettings
        Get
            If _instance Is Nothing Then
                _instance = New InitSettings
            End If
            Return _instance
        End Get
        Set(ByVal Value As InitSettings)
            _instance = Value
        End Set
    End Property

    ' ''' <summary>
    ' ''' 配置ファイルのパスを取得
    ' ''' </summary>
    'Private Shared Function GetSettingPath() As String
    '    Dim p As String = Path.Combine( _
    '       My.Application.Info.DirectoryPath _
    '        + "\feed.config")
    '    Return p
    'End Function

    ''' <summary>
    ''' 設定をXMLファイルから読み込み復元する
    ''' </summary> 
    ''' <param name="strSettingPath">配置ファイルのパス</param>
    ''' <returns>読み込み結果</returns>
    ''' <exception cref="Exception">読み込み失敗</exception>
    Public Shared Function LoadFromXmlFile(ByVal strSettingPath As String) As Boolean
        Dim p As String = strSettingPath
        Try

            Dim fs As New FileStream(p, FileMode.Open, FileAccess.Read)
            Dim xs As New System.Xml.Serialization.XmlSerializer(GetType(InitSettings))
            '読み込んで逆シリアル化する
            Dim obj As Object = xs.Deserialize(fs)

            Instance = CType(obj, InitSettings)

            fs.Close()
            fs.Dispose()
            fs = Nothing
            xs = Nothing

            Return True
        Catch ex As Exception
            SystemError.SystemLog(ex)
            Return False
        End Try
    End Function

    ''' <summary>
    ''' 現在の設定をXMLファイルに保存する
    ''' </summary>
    ''' <param name="strSettingPath">配置ファイルのパス</param>
    ''' <returns>保存結果</returns>
    ''' <exception cref="Exception">保存失敗</exception>
    Public Shared Function SaveToXmlFile(ByVal strSettingPath As String) As Boolean
        Dim p As String = strSettingPath

        Try
            Dim fs As New FileStream( _
                p, FileMode.Create, FileAccess.Write)
            Dim xs As New System.Xml.Serialization.XmlSerializer( _
                GetType(InitSettings))
            'シリアル化して書き込む
            xs.Serialize(fs, Instance)

            fs.Close()
            fs.Dispose()
            fs = Nothing
            xs = Nothing

            Return True
        Catch ex As Exception
            SystemError.SystemLog(ex)
            Return False
        End Try
    End Function

End Class